package com.five.taotao.guide;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.five.taotao.Application.MyApplication;
import com.five.taotao.R;
import com.five.taotao.guide_mine.MoneyActivity;
import com.five.taotao.guide_mine.MyCollectActivity;
import com.five.taotao.guide_mine.MyNoteActivity;
import com.five.taotao.guide_mine.MyPointActivity;
import com.five.taotao.guide_mine.OrdersActivity;
import com.five.taotao.guide_mine.PersonalInforActivity;
import com.five.taotao.guide_mine.ShopCarActivity;
import com.five.taotao.login.LoginActivity;
import com.five.taotao.login.RegisterActivity;
import com.five.taotao.product_details.MessageActivity;
import com.five.taotao.setup.InstallActivity;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * A simple {@link Fragment} subclass.
 */
public class MineFragment extends Fragment {


    @BindView(R.id.login)
    TextView login;
    @BindView(R.id.register)
    TextView register;
    @BindView(R.id.shopcar)
    TextView shopcar;
    @BindView(R.id.collect)
    TextView collect;
    @BindView(R.id.money)
    TextView money;
    @BindView(R.id.item1)
    TextView item1;
    @BindView(R.id.pay)
    TextView pay;
    @BindView(R.id.put)
    TextView put;
    @BindView(R.id.send)
    TextView send;
    @BindView(R.id.commit)
    TextView commit;
    @BindView(R.id.back)
    TextView back;
    @BindView(R.id.item2)
    TextView item2;
    @BindView(R.id.item5)
    TextView item5;
    @BindView(R.id.item6)
    TextView item6;
    @BindView(R.id.item7)
    TextView item7;
    @BindView(R.id.item8)
    TextView item8;
    private View v;
    @BindView(R.id.item3)
    TextView item3;
    @BindView(R.id.item4)
    TextView item4;
    @BindView(R.id.header)
    CircleImageView header;

    MyApplication application;

    @BindView(R.id.minegooddetail)
    LinearLayout minegooddetail;
    @BindView(R.id.login_register)
    LinearLayout login_register;
    @BindView(R.id.user_id)
    TextView user_id;
    private String userid;
    private SharedPreferences sp;
    public MineFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (v == null) {
            v = inflater.inflate(R.layout.fragment_mine, container, false);
            ButterKnife.bind(this, v);
        }
        application = ((MyApplication) getActivity().getApplicationContext());
        sp=getActivity().getSharedPreferences("user", Context.MODE_PRIVATE);
//        userid=sp.getString("userId","");
        userid=MyApplication.userId;
        if(userid==""){
            user_id.setVisibility(View.GONE);
            login_register.setVisibility(View.VISIBLE);
            header.setImageResource(R.drawable.social_default_my_slide);
        }else{
            header.setImageResource(R.drawable.personal_girl);
            minegooddetail.setVisibility(View.VISIBLE);
            login_register.setVisibility(View.GONE);
            user_id.setVisibility(View.VISIBLE);
            user_id.setText("欢迎，" + userid);
        }
        return v;
    }

    @Override
    public void onStart() {
        super.onStart();
        userid=MyApplication.userId;
        if(userid==""){
            user_id.setVisibility(View.GONE);
            login_register.setVisibility(View.VISIBLE);
            header.setImageResource(R.drawable.social_default_my_slide);
        }else{
            header.setImageResource(R.drawable.personal_girl);
            minegooddetail.setVisibility(View.VISIBLE);
            login_register.setVisibility(View.GONE);
            user_id.setVisibility(View.VISIBLE);
            user_id.setText("欢迎,ID:" + userid);
        }
        if (application.bitmap() != null) {
            header.setImageBitmap(application.bitmap());
        }
    }

    @OnClick({R.id.login, R.id.register, R.id.shopcar, R.id.collect, R.id.money})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.login:
                Intent intent = new Intent(getActivity(), LoginActivity.class);
                startActivityForResult(intent, 1);
                break;
            case R.id.register:
                Intent intent1 = new Intent(getActivity(), RegisterActivity.class);
                startActivity(intent1);
                break;
            case R.id.shopcar:
                startLoginedorLogoutedActivity(ShopCarActivity.class,LoginActivity.class);
                break;
            case R.id.collect:
                startLoginedorLogoutedActivity(MyCollectActivity.class,LoginActivity.class);
                break;
            case R.id.money:
                startLoginedorLogoutedActivity(MoneyActivity.class,LoginActivity.class);
                break;
        }

    }

    @OnClick({R.id.item3, R.id.item4, R.id.item2, R.id.item5, R.id.item6, R.id.item7, R.id.item8})
    public void onClick1(View view) {
        switch (view.getId()) {
            case R.id.item2:
                break;
            case R.id.item5:
                break;
            case R.id.item6:
                break;
            case R.id.item7:
                break;
            case R.id.item8:
                break;
            case R.id.item3:
                startLoginedorLogoutedActivity(MyPointActivity.class,LoginActivity.class);
                break;
            case R.id.item4:
                startLoginedorLogoutedActivity(MyNoteActivity.class,LoginActivity.class);
                break;

        }
    }


    @OnClick({R.id.item1, R.id.pay, R.id.put, R.id.send, R.id.commit, R.id.back})
    public void onClick2(View view) {
        switch (view.getId()) {
            case R.id.item1:
                startLoginedorLogoutedActivity(OrdersActivity.class,LoginActivity.class);
                break;
            case R.id.pay:
                Intent intent1 = new Intent(getActivity(), OrdersActivity.class);
                startActivity(intent1);
                break;
            case R.id.put:
                Intent intent2 = new Intent(getActivity(), OrdersActivity.class);
                startActivity(intent2);
                break;
            case R.id.send:
                Intent intent3 = new Intent(getActivity(), OrdersActivity.class);
                startActivity(intent3);
                break;
            case R.id.commit:
                Intent intent4 = new Intent(getActivity(), OrdersActivity.class);
                startActivity(intent4);
                break;
            case R.id.back:
                Intent intent5 = new Intent(getActivity(), OrdersActivity.class);
                startActivity(intent5);
                break;

        }
    }
    //点击头像跳转到可退出、登录界面
    @OnClick(R.id.header)
    public void selfInfor(View v) {
//        userid=sp.getString("userId","");
        userid=MyApplication.userId;
        if(userid==""){
            Intent intent = new Intent(getActivity(), LoginActivity.class);
            startActivity(intent);
        }else {
            Intent intent = new Intent(getActivity(), PersonalInforActivity.class);
            startActivity(intent);
        }
    }

    @OnClick(R.id.setup)
    public void setup(View v) {
        Intent intent = new Intent(getActivity(), InstallActivity.class);
        startActivity(intent);
    }

    @OnClick(R.id.message)
    public void message() {
        Intent intent = new Intent(getActivity(), MessageActivity.class);
        startActivity(intent);
    }

    public Bitmap getDiskBitmap(String pathString) {
        Bitmap bitmap = null;
        try {
            File file = new File(pathString);
            if (file.exists()) {
                bitmap = BitmapFactory.decodeFile(pathString);

            }
        } catch (Exception e) {
        }
        return bitmap;
    }

//    @Override
//    public void onActivityResult(int requestCode, int resultCode, Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//        if (requestCode == 1 && resultCode == 1) {//登录成功
//            String tmp = data.getStringExtra("userId");
//            MyApplication.userId = tmp;//将获取到的id赋值给全局变量
//            header.setImageResource(R.drawable.personal_girl);
//            minegooddetail.setVisibility(View.VISIBLE);
//            login_register.setVisibility(View.GONE);
//            user_id.setVisibility(View.VISIBLE);
//            user_id.setText("欢迎，" + tmp);
//        }else if(requestCode==2&&resultCode==2){//退出登录
//            user_id.setVisibility(View.GONE);
//            login_register.setVisibility(View.VISIBLE);
//            header.setImageResource(R.drawable.social_default_my_slide);
//            //将本地的userid值更改
//            sp=getActivity().getSharedPreferences("user",Context.MODE_PRIVATE);
//            SharedPreferences.Editor  editor=sp.edit();
//            editor.putString("userId", "");
//            editor.commit();
//            MyApplication.userId="";
//        }
//
//    }

    /**
     * 跳转界面公用方法
     * @param
     */
    private void startLoginedorLogoutedActivity(Class clzssin,Class clzssout){
        Intent intent;
//        userid=sp.getString("userId","");
        userid=MyApplication.userId;
        if(userid==""){//未登录
            intent=new Intent(getActivity(),clzssout);
            startActivity(intent);
        }else{//已经登录
            intent=new Intent(getActivity(),clzssin);
            startActivity(intent);
        }
    }

}
